package com.dishdex.menuhub.module.module.category.service;

import com.dishdex.menuhub.module.module.category.cache.CategoryCache;
import com.dishdex.menuhub.module.module.category.entity.Category;
import com.dishdex.menuhub.module.module.category.mapper.CategoryMapper;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    public Category getById(Long id) {
        if (id == null) {
            throw new RuntimeException("id不能为空");
        }
        if (id <= 0) {
            throw new RuntimeException("id需要大于0");
        }
        Category category = categoryMapper.getById(id);
        if (category == null) {
            throw new RuntimeException("菜品分类不存在");
        }
        return category;
    }

    public Category extractById(Long id) {
        if (id == null) {
            throw new RuntimeException("id不能为空");
        }
        if (id <= 0) {
            throw new RuntimeException("id需要大于0");
        }
        Category category = categoryMapper.extractById(id);
        if (category == null) {
            throw new RuntimeException("菜品分类不存在");
        }
        return category;
    }

    public Long insert(
            String categoryName,
            String categoryImage,
            Long parentId) {
        if (categoryName == null || categoryName.trim().isEmpty()) {
            throw new RuntimeException("categoryName不能为空");
        }
        if (categoryName.trim().length() > 100) {
            throw new RuntimeException("categoryName的长度不能超过1000");
        }
        if (categoryImage == null || categoryImage.trim().isEmpty()) {
            throw new RuntimeException("categoryImage标不能为空");
        }
        if (categoryImage.trim().length() > 500) {
            throw new RuntimeException("categoryImage的长度不能超过500");
        }
        if (parentId == null) {
            throw new RuntimeException("菜品分类父Id不能为空");
        }

        if (parentId < 0) {
            throw new RuntimeException("parentId不能小于0");
        }

        if (parentId != 0 && categoryMapper.getById(parentId) == null) {
            throw new RuntimeException("parentId所对应的菜品分类不存在");
        }
        int temp = (int) (System.currentTimeMillis() / 1000);
        Category category = new Category()
                .setCategoryName(categoryName.trim())
                .setCategoryImage(categoryImage.trim())
                .setParentId(parentId)
                .setCreateTime(temp)
                .setUpdateTime(temp)
                .setIsDeleted((byte) 0);
        categoryMapper.insert(category);
        return category.getId();
    }

    @Resource
    private CategoryCache categoryCache;

    public Long update(
            Long id,
            String categoryName,
            String categoryImage,
            Long parentId) {
        if (id <= 0) {
            throw new RuntimeException("id需要大于0");
        }
        if (categoryName == null || categoryName.trim().isEmpty()) {
            throw new RuntimeException("菜品分类名称不能为空");
        }
        if (categoryName.trim().length() > 100) {
            throw new RuntimeException("categoryName的长度不能超过1000");
        }
        if (categoryImage == null || categoryImage.trim().isEmpty()) {
            throw new RuntimeException("菜品分类图标不能为空");
        }
        if (categoryImage.trim().length() > 500) {
            throw new RuntimeException("categoryImage的长度不能超过500");
        }
        if (parentId == null) {
            throw new RuntimeException("菜品父Id不能为空");
        }
        if (parentId < 0) {
            throw new RuntimeException("parentId不能小于0");
        }

        if (parentId != 0 && categoryMapper.getById(parentId) == null) {
            throw new RuntimeException("parentId所对应的菜品分类不存在");
        }

        if (categoryMapper.getById(id) == null) {
            throw new RuntimeException("更新的菜品分类不存在");
        }
        int temp = (int) (System.currentTimeMillis() / 1000);
        Category category = new Category()
                .setId(id)
                .setCategoryName(categoryName.trim())
                .setCategoryImage(categoryImage.trim())
                .setParentId(parentId)
                .setUpdateTime(temp);
        categoryMapper.update(category);
        categoryCache.updateCache(id);
        return category.getId();
    }

    public int delete(Long id) {
        if (id == null) {
            throw new RuntimeException("id不能为空");
        }
        if (id <= 0) {
            throw new RuntimeException("id需要大于0");
        }
        if (categoryMapper.getById(id) == null) {
            throw new RuntimeException("要删除的菜品分类不存在");
        }
        categoryCache.updateCache(id);
        return categoryMapper.deleteById(id);
    }

    public List<Category> getAll(Long id) {
        if (id != null) {
            if (id <= 0) {
                throw new RuntimeException("菜品分类Id需要大于0");
            }
            Category category = categoryMapper.getById(id);
            if (category == null) {
                throw new RuntimeException("该分类不存在");
            }

            if (category.getParentId() != 0) {
                throw new RuntimeException("该分类不是一级类目");
            }

        }
        List<Category> categoryList = categoryMapper.getAll(id);

        if (categoryList == null) {
            return new ArrayList<>();
        }
        return categoryList;
    }

    public List<Category> getMore(Long id) {
        if (id == null) {
            throw new RuntimeException(("菜品分类Id不能为空"));
        }
        Category category = categoryMapper.getById(id);
        if (category == null) {
            throw new RuntimeException("该分类不存在");
        }
        return categoryMapper.getMore(id);
    }

    public Category getCategory(Long categoryId) {
        Category category = categoryCache.getCategoryByCategoryId(categoryId);
        if (category == null) {
            throw new RuntimeException("该菜品的菜品分类不存在");
        }
        return category;
    }

    public StringBuilder getIds(String keyword) {
        if (keyword == null || keyword.trim().isEmpty()) {
            return new StringBuilder();
        }
        List<Long> ids = categoryMapper.getIds(keyword);
        if (ids == null) {
            return new StringBuilder();
        }
        StringBuilder sb = new StringBuilder();
        for (Long id : ids) {
            if (sb.length() > 0) {
                sb.append(",");
            }
            sb.append(id);
        }
        return sb;
    }

    public List<Long> getLevelOneIds() {
        return categoryMapper.getLevelOneIds();
    }

    public List<Long> getLevelTwoIds() {
        return categoryMapper.getLevelTwoIds();
    }


    public void checkCategoryId(Long categoryId) {
        if (categoryId == null) {
            throw new RuntimeException("categoryId不能为空");
        }
        if (categoryId <= 0) {
            throw new RuntimeException("categoryId需要大于0");
        }
        if (categoryMapper.getById(categoryId) == null) {
            throw new RuntimeException("菜品分类不存在");
        }
    }
}